/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 *  
 *   第一步导入telewiki2/resource/common/js/enterpriseSelector.js和所需dwr的js
 *   调用弹出模式框 showDlg
 *   在弹出页面上点击查询按钮调用getSearchEnterprise 方法 返回所需值
 *   最后在弹出页面点击确定 返回值到起始页面
 * 
 */
	var jsUrls=new Array();
	jsUrls[0]=__ctxPath+"/dwr/engine.js";
	jsUrls[1]=__ctxPath+"/dwr/util.js";
	jsUrls[2]=__ctxPath+"/dwr/interface/colorProductManager.js";
	$importJs(jsUrls,function(){
	});

var colorProductSelector={

	userType:1,//2,3=customer,1=admin user 默认为2,3
	
	selectMode:0,//选择模式,0=单选,1=多选  默认为单选
	
	pageNum:1,//当前页
	
	productName:'',// 产品名称
	
	productCode:'',//联系人
	
	modelType:'',
	
	textId:null,//callback function
	
	textValue:null,//callback function
	textInfo:null,
	typre:'',
	//userSelector
	showDlg:function (div,modelType,type){

	$('#createId').empty();
	var imgSrc;
	var productName;
	var brandName;
	var categoryName;
	var colorName;
	var price;
	var prosizeId;
	var productId;
	var colorProductId;
		if(typeof(modelType)=='undefined'){
			modelType='';
		}
		this.type=type;
		this.modelType=modelType;
		this.userDlg=createOverlayDlg("colorProductSelector",{
			title:'用户选择',
			width:820,
			height:500,
			buttons:{
				"确 定":function (){	
			
			var imgSrcs=$("input:hidden[name='imgSrc']");
			var productNames=$("input:hidden[name='productName']");
			var colorProductIds=$("input:radio[name='colorProductId']");
			var categoryNames=$("input:hidden[name='categoryName']");
			var colorNames=$("input:hidden[name='colorName']");
			var prices=$("input:hidden[name='price']");
			//var prosizeIds=$("input:hidden[name='colorProductId']");
			var productIds=$("input:hidden[name='productId']");
			var brandNames=$("input:hidden[name='brandName']");
			$('#stocksButton').removeAttr("disabled");
			for(var i=0;i<colorProductIds.size();i++){	
				if(colorProductIds[i].checked){	
					imgSrc=imgSrcs[i].value;
					productName=productNames[i].value;
					colorProductId=colorProductIds[i].value;
					brandName=colorProductIds[i].value;
					categoryName=categoryNames[i].value;
					colorName=colorNames[i].value;
					price=prices[i].value;
					productId=productIds[i].value;
					brandName=brandNames[i].value;
					break;
					}
			}
			var arry;
			$.ajax({
				 type: "post",
				  url:__ctxPath+"/storage/bbase/colorProductDialog.do?cmd=addBatchAndSize",
				  data:{colorProductId:colorProductId},
				  async:false,
				  success:function(data){
				   arry=data.split(',');
				  },
				  error:function(XMLHttpRequest){
					  alert(XMLHttpRequest);
					  }
			});
			var creatLi="<h3>产品尺码</h3><ul class='size_list'>";
		for(var j=0;j<arry.length;j++){
			var value=arry[j];
			var newArry=value.split("-");
			creatLi=creatLi+"<li><input type='checkbox' id='"+newArry[0]+"' value='"+newArry[0]+"' name='prosizeId'/>" +
		"&nbsp;"+newArry[1]+"&nbsp;" +
		"请输入采购数量<input type='text' id='"+newArry[0]+"count' name='count'/>" +
		"<input type='hidden' value="+productId+" name='productId'/>" +
		"<input type='hidden'  value="+colorProductId+" name='colorProductId' id='"+newArry[0]+"colorProduct'/>" +
		"<input type='hidden'  value="+newArry[3]+" name='batchCode' id='"+newArry[0]+"batch'/>" +
		"</li>";
		}
		var createDiv="<div class='inventory_rt'>"+
		"<div class='inventory_rt'>"+
			"<div class='inventory_con'>"+
				"<div class='title'><strong>新增采购</strong></div>"+
				"<div class='purchase_info'>"+
				"</div>"+
				"<div class='pro_item'>"+
					"<img src='"+__ctxPath+"/"+imgSrc+"' />"+
					"<p>"+
						"<strong>名称：</strong>"+productName+"<br />"+
						"<strong>品牌：</strong>"+brandName+"<br />"+
						"<strong>分类：</strong>"+categoryName+"<br />"+
						"<strong>颜色：</strong>"+colorName+"<br />"+
						"<strong>网站价格：</strong>￥"+price+""+
					"</p>" +creatLi
			
				"</dl>";

			$('#'+div).append(createDiv);
		
			$(this).dialog("close");
			},
				"取 消":function (){$(this).dialog("close");}
			},
			url:__ctxPath+"/storage/bbase/colorProductDialog.do?decorator=blank&modelType="+modelType+"&type="+type
		});
	},

	getSearchColorProduct:function(pageNum){

		this.pageNum=pageNum;	
	 	DWREngine.setAsync(false);
	 	colorProductManager.getByProductNameOrPrductCode(this.productName,this.productCode,this.modelType,pageNum,this.type,fillTableToFunction);
	 	//fillTableToFunction(null);
	},
	
	searchAction:function(){
		
		colorProductSelector.productName=$("#_productName").val();
		colorProductSelector.productCode=$("#_productCode").val();
		colorProductSelector.getSearchColorProduct(1);
	}
}
function fillTableToFunction(colorProductList){
	DWRUtil.removeAllRows("_colorProduct_data_body"); 

	if (colorProductList.length ==null || colorProductList.length==0) {
		
		 var tr =document.createElement("tr");
		 var td=document.createElement("td");
	
		 td.innerHTML="<font color='red'>没有颜色产品.</font>";
		 tr.appendChild(td);
	
		 
	$("#_colorProduct_data_body").append(tr);
	$("#showname").hide();
	$("#page").hide();

	
	} else { 
		$("#showname").show();
		$("#page").show();
		DWRUtil.addRows("_colorProduct_data_body", colorProductList, cellToFunctions); 
	}
	
}

function _nextTo(){
	var colorProductId=$("input:radio[name='colorProductId']")
	if(colorProductId.size()<10){
		alert('已经是最后一页');
		return;//尾页	
		}
	colorProductSelector.getSearchColorProduct(parseInt($('#_current_page_num').val())+1);
	$('#_current_page_num').attr("value",parseInt($('#_current_page_num').val())+1);
}




function _previousTo(){

	if(parseInt($("#_current_page_num").val())<=1){//首页
		$("_current_page_num").value=1;
		alert('首页');
		return;
	}
	colorProductSelector.getSearchColorProduct(parseInt($('#_current_page_num').val())-1);
	$('#_current_page_num').attr("value",parseInt($('#_current_page_num').val())-1);
}

//fill the table cell
var cellToFunctions = [
						function(colorProduct) {
							var selectType="";
							if(this.selectMode=0){
								selectType="radio";
							}				
							var str="<input type='radio' name='colorProductId' value='"+colorProduct.colorProdId+"' />" +
									"<input type='hidden' name='productId' value='"+colorProduct.product.productId+"' />"+
									"<input type='hidden' name='commondityId"+colorProduct+"' value='"+colorProduct.product.productId+"' />";
							var span=document.createElement("span");
							span.innerHTML=str;
							return span;
						}, 
					    function(colorProduct) {
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='imgSrc' value='"+colorProduct.mainImgPath +"'/>" +
					    	 		"<img style='padding-top: 8px' width='80' height='80' src="+__ctxPath+"/"+colorProduct.mainImgPath +"'/>";
					    	 var td=document.createElement("td");	
					    	 span.appendChild(td);					    		    	 
							 return span;
						},
					    function(colorProduct) {
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='productName' value='"+colorProduct.product.productName +"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=colorProduct.product.productName;	
					    	 span.appendChild(td);					    		    	 
							 return span;
						},
						function(colorProduct) {											 
							var span=document.createElement("span"); 
							 span.innerHTML=colorProduct.product.productCode;						
							return span;
						}, 
						function(colorProduct) {			
								var span=document.createElement("span"); 
								span.innerHTML=colorProduct.batchCount;	
								return span;	
						},
						function(colorProduct) {		
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='brandName' value='"+colorProduct.product.productBrand.brandName +"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=colorProduct.product.productBrand.brandName;	
					    	 span.appendChild(td);					    		    	 
							 return span;	
						},
						function(colorProduct) {			
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='categoryName' value='"+colorProduct.product.category.categoryName +"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=colorProduct.product.category.categoryName;	
					    	 span.appendChild(td);					    		    	 
							 return span;	
						},	
						function(colorProduct) {		
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='colorName' value='"+colorProduct.productColor.colorName+"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=colorProduct.productColor.colorName;	
					    	 span.appendChild(td);					    		    	 
							 return span;
						},	
						function(colorProduct) {	
						   	 var span=document.createElement("span");
					    	 span.innerHTML="<input type='hidden' name='price' value='"+colorProduct.product.price+"'/>";
					    	 var td=document.createElement("td");	
							 td.innerHTML+=colorProduct.product.price;	
					    	 span.appendChild(td);		
					    	 return span;
						}
				 ];

					 

